package com.android.woodpecker;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class FreePlayGameScreen extends Activity {
	RelativeLayout gameLayout;
	TextView timeDisplay;
	TextView scoreDisplay;
	int score=0;
	AnimationSet blinking;
	AlphaAnimation inblink, outblink;
	int totaltime = 24000;
	int secondsLeft;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		gameLayout = new RelativeLayout(this);

		timeDisplay = new TextView(this);
		scoreDisplay = new TextView(this);
		

		
		inblink = new AlphaAnimation(0.0f,1.0f);
		inblink.setDuration(300);
		
		outblink = new AlphaAnimation(1.0f,0.0f);
		outblink.setDuration(300);
		outblink.setStartOffset(400);
		
		blinking = new AnimationSet(true);
		blinking.addAnimation(inblink);
		blinking.addAnimation(outblink);
		
		secondsLeft = 0;

		new CountDownTimer(totaltime, 100) {

			public void onTick(long ms) {
				if (Math.round((float)ms / 1000.0f) != secondsLeft)
		         {  
		             secondsLeft = Math.round((float)ms / 1000.0f); 
		             if(secondsLeft >= 10){
				    	 timeDisplay.setText("" + secondsLeft);
			    	 }else{
			    	 timeDisplay.setText("0" + secondsLeft);	 
			    	 }
		             if(secondsLeft <= 15){
		            	 timeDisplay.setVisibility(View.VISIBLE);
			    		 timeDisplay.setTextColor(Color.RED);
			    		 timeDisplay.startAnimation(blinking);
			    		 timeDisplay.postDelayed(new Runnable(){

							@Override
							public void run() {
								// TODO Auto-generated method stub
								timeDisplay.setVisibility(View.INVISIBLE);
							}
			    			 
			    		 }, blinking.getDuration());
			    	 }
		         }
		         Log.i("test","ms="+ms+" till finished="+secondsLeft );

			}

			public void onFinish() {
				timeDisplay.setText("done!");
				Intent resultIntent=new Intent(FreePlayGameScreen.this,FreePlayResult.class);
				resultIntent.putExtra("total", (int)score);
				startActivity(resultIntent);
				
			}
		}.start();

		LayoutParams timeParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		timeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
				RelativeLayout.TRUE);
		timeDisplay.setTextSize(24);
		timeDisplay.setId(100);
		
		gameLayout.addView(timeDisplay, timeParams);
		
		LayoutParams scoreParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
		scoreParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
		scoreDisplay.setTextSize(24);
		scoreDisplay.setId(101);
		scoreDisplay.setText(""+score);
		
		gameLayout.addView(scoreDisplay, scoreParams);

	}

}
